<template>
  <header id="header" class="header" :class="{fixed: headerFixed}">
    <div class="main-center">
      <div class="left">
        <a href="/" class="logo">
          <img src="~/assets/img/logo-SC.png" alt="">
        </a>
        <nuxt-link v-for="item of routes" :key="item.path" :to="item.path" class="btn">
          {{ item.name }}
          <span />
        </nuxt-link>
      </div>
      <div class="right">
        <el-input
          v-model="search"
          placeholder="请输入关键词"
          suffix-icon="el-icon-search"
        />
        <div v-if="!userData" class="login-panel" @click="isShowDialog_login = true">
          <span ref="signIn" class="login sign-in" @click="loginType = 'signIn'">
            登录
          </span>
          <a>|</a>
          <span class="login sign-up" @click="loginType = 'signUp'">
            注册
          </span>
        </div>
        <div v-if="userData" class="ctrl-panel">
          <nuxt-link class="iconfont icon-ic-write btn" to="/edit" />
          <nuxt-link class="iconfont icon-ic-news btn" to="/notification">
            <el-badge v-show="haveNotification" is-dot class="item" />
          </nuxt-link>
          <a class="btn">
            <user-avatar />
          </a>
        </div>
      </div>
    </div>

    <el-dialog :visible.sync="isShowDialog_login" :show-close="false" center :lock-scroll="false">
      <el-tabs v-model="loginType">
        <el-tab-pane label="登录" name="signIn">
          <sign-in
            :phone-number="phoneNumber"
            @showForgetPassword="showForgetPassword"
            @onClose="isShowDialog_login = false"
          />
        </el-tab-pane>
        <el-tab-pane label="注册" name="signUp">
          <sign-up @toSignIn="toSignIn" />
        </el-tab-pane>
      </el-tabs>

      <div class="logo-panel">
        <div class="decorate">
          d
        </div>
        <div class="decorate">
          d
        </div>
        <div class="decorate">
          d
        </div>
        <div class="logo">
          <img src="~/assets/img/logo.png" alt="">
        </div>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="isShowDialog_forgetPassword" :show-close="false" center :lock-scroll="false">
      <forget-password @toSignIn="toSignIn" @onClose="isShowDialog_forgetPassword = false" />
    </el-dialog>
  </header>
</template>

<script>
import { throttle } from '~/assets/js/util'

import SignIn from '~/components/header/SignIn'
import SignUp from '~/components/header/SignUp'
import ForgetPassword from '~/components/header/ForgetPassword'
import UserAvatar from '~/components/header/UserAvatar'

export default {
  components: {
    SignIn,
    SignUp,
    ForgetPassword,
    UserAvatar
  },
  data() {
    return {
      haveNotification: false,
      phoneNumber: '',
      isShowDialog_login: false,
      isShowDialog_forgetPassword: false,
      loginType: 'signIn',
      search: '',
      routes: [{
        name: '博客',
        path: '/'
      }, {
        name: '问答',
        path: '/problem'
      }, {
        name: '项目',
        path: '/project'
      }],

      headerFixed: false,
      beforeScrollTop: 0
    }
  },
  computed: {
    userData() {
      return this.$store.state.user.userData
    }
  },
  created() {
    window.addEventListener('scroll', throttle(() => {
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop

      if (scrollTop > 200) {
        const delta = scrollTop - this.beforeScrollTop

        if (delta > 0) {
          this.headerFixed = false
        } else if (delta < 0) {
          this.headerFixed = true
        }

        console.log(scrollTop)

        // delta > 0 'down'
        // delta < 0 'up'
        this.beforeScrollTop = scrollTop
      } else {
        this.headerFixed = false
      }
    }, 100))
  },
  methods: {
    showForgetPassword() {
      this.isShowDialog_login = false
      this.isShowDialog_forgetPassword = true
    },
    toSignIn(phoneNumber) {
      this.phoneNumber = phoneNumber
      this.isShowDialog_login = true
      this.loginType = 'signIn'
    },
    toSignOut() {
      // 移除用户数据
      this.$store.commit('removeUserDataMutation')
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/css/public";
/*.user-avatar-back:hover .dropdown {*/
  /*display: block!important;*/
/*}*/

.header {
  width: 100%;
  background: #fff;
  border-bottom: 1px solid $theme-color-border;
  &.fixed {
    position: fixed;
    top: 0;
  }
  .main-center {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 70px;
    font-size: 14px;
    background: #fff;
    .left {
      display: flex;
      align-items: center;
      .logo {
        margin-left: 26px;
        img {
          display: block;
          width: 146px;
          height: 31px;
        }
      }
      .btn {
        color: #666;
        margin-left: 40px;
        height: 70px;
        line-height: 70px;
        overflow: hidden;
        transition: $theme-transition;

        span {
          display: none;
          width: 6px;
          height: 6px;
          border-radius: 6px;
          background: $theme-color;
          margin: -3px auto;
        }

        &:hover,
        &.nuxt-link-exact-active {
          color: $theme-color;
        }

        &.nuxt-link-exact-active {
          font-weight: bold;
          span {
            display: block;
          }
        }
      }
    }
    .right {
      display: flex;
      align-items: center;

      .el-input /deep/ {
        width: 240px;
        font-size: 12px;
        margin-right: 30px;
        .el-input__inner,
        .el-input__icon {
          height: 34px;
          line-height: 34px;
        }
        .el-input__inner {
          background: $theme-color-background;
          border-radius: 34px;
          border: 1px solid #fff;

          &:focus {
            border-color: $theme-color;
          }
        }
      }
      .login-panel {
        a {
          color: #999;
        }
        span.login {
          cursor: pointer;
          transition: $theme-transition;

          &:hover {
            color: $theme-color;
          }
        }
      }

      .ctrl-panel {
        display: flex;
        align-items: center;
        a.btn {
          cursor: pointer;
          width: 70px;
          height: 70px;
          line-height: 70px;
          text-align: center;
          font-size: 18px;
          transition: $theme-transition;

          &.icon-ic-news {
            font-size: 22px;

            /deep/ .el-badge {
              vertical-align: top;

              .el-badge__content {
                position: absolute;
                top: 20px;
                left: 0;
              }
            }
          }

          &:hover {
            background: $theme-color-background;
            color: $theme-color;
          }
        }
      }
    }
  }
  /deep/ .el-dialog__wrapper {
    display: flex;
    justify-content: center;
    align-items: center;

    .el-dialog {
      width: 422px;
      border: 1px solid $theme-color-border;
      margin: 0!important;

      .el-dialog__header {
        display: none;
      }
      .el-dialog__body {
        padding: 10px 24px 30px;
        .logo-panel {
          position: absolute;
          width: 70px;
          top: -30.31px;
          left: 50%;
          margin-left: -35px;

          div.decorate {
            width: 35px;
            height: 60.62px;
            border-top: 1px solid $theme-color-border;
            border-bottom: 1px solid $theme-color-border;
            color: #fff;
            background: #fff;
          }
          div.decorate:nth-child(1) {
            margin: 0 auto;
          }

          div.decorate:nth-child(2),
          div.decorate:nth-child(3) {
            position: absolute;
            left: 17.5px;
            top: 0;
            transform: translate(-50%,-50%);
          }

          div.decorate:nth-child(2) {
            transform: rotate(60deg);
          }

          div.decorate:nth-child(3) {
            transform: rotate(300deg);
          }

          .logo {
            width: 70px;
            height: 60.62px;
            position: absolute;
            top: 0;
            left: 0;
            display: flex;
            justify-content: center;
            align-items: center;

            img {
              display: block;
              width: 40px;
              height: 34px;
            }
          }
        }

        .el-tabs {
          .el-tabs__header {
            margin: 0 0 50px;

            .el-tabs__nav-wrap {
              &::after {
                display: none;
              }

              .el-tabs__nav {
                width: 100%;
                display: flex;
                justify-content: space-between;
                .el-tabs__item {
                  color: #999;

                  &#tab-signIn {
                    margin-left: 64px;
                  }
                  &#tab-signUp {
                    margin-right: 64px;
                  }

                  &.is-active {
                    color: #222;
                  }
                }
              }
            }
          }
        }

        .el-form {
          .el-form-item {
            margin-bottom: 30px;

            .el-input__inner {
              border: none;
              border-bottom: 2px solid $theme-color-background!important;
              padding: 0 6px;
            }
            .el-form-item__error {
              color: #F07272;
              margin-left: 6px;
            }
          }
        }

        .btn-submit {
          cursor: pointer;
          color: #fff;
          width: 100%;
          background: $theme-color;
          height: 42px;
          line-height: 42px;
          margin-top: 16px;
        }
      }
    }
  }
}
</style>
